"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminJwzCtrl
 * @description
 * # AdministratorAdminJwzCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "AdministratorAdminJwzCtrl",
    function($scope, $rootScope, DataService, openApi, Util, $location, $routeParams, FormSubmit, localStorageService) {
      var id = ($scope.process_id = $routeParams.id);
      // 选择报销单明细
      var getWZlist = function() {
        var param = {};
        param.type = "物资类型";
        Util.getKeyValue(param, function(data) {
          $scope.WZTypeList = data.data;
        });
      };
      // 选择物资明细
      $scope.choosewz = function() {
        if (!$scope.param.materialType) {
          Util.Toast("请选择物资类型");
          return;
        }
        var relModuleObj = {
          materialType: $scope.param.materialType,
          flag: 1
        };
        Util.saveSelectLists($scope.param.details);
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("administrator/choosejwz");
      };
      // 选择签发人
      $scope.choosesigners = function() {
        // 清掉关联的编号
        Util.savaParamForReview($scope.param, id);
        $location.path("/chooseuser/chooseone");
      };
      // 选择审批人
      $scope.choosepeople = function() {
        localStorageService.set("chooseUsrs", $scope.param.aduitUserIds);
        Util.savaParamForReview($scope.param, id);
        // 里面包含了title
        $location.path("userdefined/choosePerson/");
        // $location.path("chooseuser/index/getPageListByAccount");
      };
      $scope.getusers = function() {
        // type = [自定义流程审批人,通用合同审批人,借物资审批人,还物资审批人] 根据流程不同写死
        // parent = 用户选择类型的值
        var param = {
          type: "通用合同审批人",
          parent: $scope.param.materialType
        };
        DataService.getData(openApi.getlistkeyvalues, param).then(
          function(data) {
            if (data) {
              var user = data[0];
              $scope.param.aduitUserNames = user && user["key"];
              $scope.param.aduitUserIds = user && user["value"];
            }
          }
        );
      }; 
      var checkMaterial = function() {
        var check = true;
        $scope.param.details.forEach(function(item, index) {
          if (!Util.checkrlNum(item.total)) {
            check = false;
            return false;
          }
        });
        return check;
      };
      var getAllmaterial = function() {
        var details = [];
        details = $scope.param.details.map(function(item, index) {
          return {
            mid: item.pkId || item.mid,
            mname: item.mname || item.materialName,
            specifications: item.specifications,
            total: parseFloat(item.total,10)
          };
        });
        return JSON.stringify(details);
      };
      $scope.submit = function(operationId) {

        $scope.param.materialDetails = getAllmaterial();
        // 底部类型end
        var options = {};
        // 流程id
        options.id = id;
        // 根据operationId判断是草稿还是提交
        options.operationId = operationId;
        options.urlParam = openApi.borrowMaterial;
        options.param = angular.copy($scope.param, {});
        delete options.param.details;
        delete options.param.users;
        FormSubmit.submit(options,$scope);
      };
      $scope.validate = function() {
        if (!$scope.param.materialType) {
          Util.Toast("请选择物资类型	");
          return;
        }
        if (!$scope.param.sender) {
          Util.Toast("请选择签发人");
          return;
        }
        if (!$scope.param.borrowReason) {
          Util.Toast("请输入借用事由");
          return;
        }
        if (
          $scope.param.relatedModuleName &&
          $scope.param.relatedModuleName !== "无"
        ) {
          if (!$scope.param.moduleNo) {
            Util.Toast("请选择模块编号");
            return;
          }
        }
        var check = checkMaterial();
        if (!check) {
          Util.Toast("借用数量为正整数");
          return;
        }

        if (!$scope.validation.passvalidation) {
          Util.Toast($scope.validation.tip);
          return;
        }
        return true;
      };
      // 删除证件信息列表
      $scope.deletedItem = function(index) {
        $scope.param.details.splice(index, 1);
        $scope.index = -1;
      };
      $scope.swipe = function(index) {
        $scope.index = index;
      };
      $scope.resetlist = function(index) {
        $scope.index = -1;
      };
      var init = function() {
        // 获取回显的详细信息
        getWZlist();
        Util.getDetailData(id, function(data) {
          $scope.param = data;
          var userInfo = localStorageService.get("userInfo");
          localStorageService.remove("userInfo");
          // 审批人
          var chooseUsrs = localStorageService.get("chooseUsrs");
          localStorageService.remove("chooseUsrs");
          // 获取选择页面返回的数据并且合并到参数中
          if(chooseUsrs){
              $scope.param.aduitUserNames =chooseUsrs.aduitUserNames||"";
              $scope.param.aduitUserIds = chooseUsrs.aduitUserIds||"";    
          }

          var details = Util.getSelectLists();
          Util.clearSelectListsh();
          // 获取选择页面返回的数据并且合并到参数中
          $scope.param.details = details || $scope.param.details || [];

          // 获取选择页面返回的数据并且合并到参数中
          var uerObj = {};
          if (userInfo) {
            uerObj.sender = userInfo.name;
            uerObj.senderId = userInfo.userId;
          }
          $scope.param = angular.extend($scope.param, uerObj);
        });
      };
      // 初始化
      init();
    }
  );
